<?php

class CandidateReviewsController extends AppController {

    var $name = 'CandidateReviews';

    function index($candidate_id) {
        $this->layout = "ajax";
        $this->CandidateReview->Candidate->recursive = -1;
        $this->set('codigo', $this->CandidateReview->Candidate->find("first", array("conditions" => array("Candidate.id" => $candidate_id), "fields" => array("Candidate.1er_nombre", "Candidate.2do_nombre", "Candidate.2do_apellido", "Candidate.1er_apellido", "Candidate.id"))));

        if ($candidate_id == "") {
            $this->Session->setFlash('No ha seleccionado Proyecto');
            $this->redirect(array('controller' => 'Proyects', 'action' => 'index'));
        } else {
            $this->set('revisiones', $this->CandidateReview->find('all', array('conditions' => array('CandidateReview.candidate_id' => $candidate_id), 'order' => array('CandidateReview.id DESC'), 'fields' => array('CandidateReview.concepto', 'CandidateReview.id', 'CandidateReview.fecha', 'CandidateReview.calificacion', 'User.id', 'User.nombre', 'User.primer_apellido', 'User.segundo_apellido'))));
        }
    }

    function add($candidate_id) {

        $this->set('departaments', $this->CandidateReview->Candidate->Departament->find('list', array('order' => 'name ASC')));

        if (empty($this->data)) {
            $this->CandidateReview->Candidate->recursive = -1;
            $this->data = $this->CandidateReview->Candidate->find("first", array("conditions" => array("Candidate.id" => $candidate_id), "fields" => array("Candidate.1er_nombre", "Candidate.tipo_ident", "Candidate.2do_nombre", "Candidate.2do_apellido", "Candidate.1er_apellido", "Candidate.id", "Candidate.direccion", "Candidate.nro_ident", "Candidate.fecha_nacimiento", "Candidate.telefono", "Candidate.candidate_id", "Candidate.genero", "Candidate.tipo", "Candidate.clase", "Candidate.minoria", 'Candidate.city_id', 'Candidate.departament_id', 'Candidate.renuncio')));
            $this->data['CandidateReview']['user_id'] = $this->Auth->user('id');
            date_default_timezone_set("America/Bogota");
            $this->data['CandidateReview']['fecha'] = date("Y-m-d-h-i-s");
            $this->set('cities', $this->CandidateReview->Candidate->City->find('list', array('order' => 'name ASC', 'conditions' => array('City.departament_id' => $this->data['Candidate']['departament_id']))));
        } else {
            if ($this->CandidateReview->saveAll($this->data)) {
                $log = $this->CandidateReview->Candidate->findLog(array('conditions' => array('Log.action' => 'edit', 'Log.user_id' => $this->Auth->user('id')), 'model' => 'Candidate', 'fields' => array('change'), 'order' => 'Log.id DESC ', 'limit' => 1));
                $txt = $log[0]['Log']['change'];
                $this->CandidateReview->UpdateAll(array('CandidateReview.cambio' => "'$txt'"), array('CandidateReview.id' => $this->CandidateReview->getLastInsertID()));
                $this->Session->setFlash('Registro guardado correctamente');
                $this->Session->setFlash('Los datos del aspirante se han guardado con éxito');
                $this->redirect(array('controller' => 'CandidateReviews', 'action' => 'index', $this->data['Candidate']['id']));
            } else {
                $this->Session->setFlash('Error guardando datos');
            }
        }
    }

}

?>
